<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of UserController
 *
 * @author zangguoping
 */
class UserController extends Controller {

    public function actionIndex() {
        $model = new User();
        $request = Yii::app()->request;
        if ($request->getParam('User')) {
            $model->attributes = $request->getParam('User');
            //print_r($request->getParam('User'));
        }
        $this->render('index', array('model' => $model));
    }

    public function actionCreate() {
        $model = new User();
        $request = Yii::app()->request;
        $this->performAjaxValidation($model);
        if ($request->getIsPostRequest()) {
            $model->attributes = $request->getPost('User');
            $upload = new UploadForm($model, 'user_face');
            $model->user_face = $upload->save();
            if ($model->save()) {
                $this->render('/layouts/message', array('message' => Yii::t('default', '添加成功')));
                exit;
            }
        }
        $this->render('create', array('model' => $model));
    }

    public function actionDelete() {
        $model = new User();
        $request = Yii::app()->request;
        echo $id = $request->getParam('id');
        if ($id) {
            if (is_array($id)) {
                foreach ($id as $uid) {
                    $model->deleteByPk($uid);
                }
            } else {
                $model->deleteByPk($id);
            }
        }
        Yii::app()->end();
    }

    public function actionPlayer() {
        $model = new UserPlayer();
        $this->render('player', array('model' => $model));
    }

    public function actionLog() {
        $model = new UserLog();
        $request = Yii::app()->request;
        $this->performAjaxValidation($model);
        if ( is_array($request->getParam('UserLog')) ) {
            $model->attributes = $request->getParam('UserLog');
            $model->validate();
        }
        $this->render('log', array('model' => $model));
    }

    protected function performAjaxValidation($model) {
        if (isset($_POST['ajax']) && $_POST['ajax'] === 'user') {
            echo CActiveForm::validate($model);
            Yii::app()->end();
        }
    }

}
